/*
ID:nacci521
PROG:milk2
LANG:C++14
*/
#include <climits>
#include <vector>
#include <utility>
#include <fstream>
#include <algorithm>
#include <iostream>

using namespace std;
/*
FFFTTTT
3 5
6 8
9 11
4
1
*/

/*
pair<int, int>
pair.first 起始时间
pair.second 结束时间
*/

struct Milking {
  int begin; // 起始挤奶的时间
  int end;   // 结束挤奶的时间
};

// 我们自定义的 Milking 的比较规则
// l < r
bool cmp(const Milking &l, const Milking &r) {
  return l.begin < r.begin;
}

// 运算符重载
// 告诉 C++ Milking 类型的比较规则
bool operator < (const Milking &l, const Milking &r) {
  return l.begin < r.begin;
}

bool operator == (const Milking &l, const Milking &r) {
  return l.begin == r.begin && l.end == r.end;
}

int main(int argc, char const *argv[])
{
  /* code */
  // 处理输入
  // N
  // begin end
  ifstream ifs("milk2.in");
  
  int n = 0;
  ifs >> n;

  // 读入剩余所有行，并把挤奶记录添加到 records
  vector<Milking> records;

  for (int i = 0; i < n; ++i) {
    int b = 0, e = 0;
    ifs >> b >> e;
    Milking m{b, e};
    records.push_back(m);
  }

  sort(records.begin(), records.end());

  for (auto r : records) {
    cout << r.begin << " " << r.end << endl;
  }

  // 所有挤奶的开始时间，都是从小到大排序的了
  int max_milking = 0, max_nomilking = 0;
  Milking curr = records.front();

  for (int i = 1; i < records.size(); ++i) {
    // 遍历 records 中的剩余时间段
    // 右侧超出的情况
    if (records[i].begin > curr.end) {
      /*
      3 4
      5 6
      */
      int t = records[i].begin - curr.end; // 一段没有牛挤奶的时间
      max_nomilking = max(max_nomilking, t); // milestone 1

      t = curr.end - curr.begin;
      max_milking = max(max_milking, t);

      curr = records[i];
    }
    else {
      if (records[i].end > curr.end) {
        // 右侧交叉
        curr.end = records[i].end;
      }
    }
  }

  int t = curr.end - curr.begin;
  max_milking = max(max_milking, t);

  cout << "Max milking: " << max_milking << endl;
  cout << "Max nomilking: " << max_nomilking << endl;
  return 0;
}
